//
// Created by Administrator on 2023/11/20.
//
#include "iostream"

using namespace std;

const int N = 310;
int a[N], dp[N][N];

int main() {
    int n;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i)
        a[i] += a[i - 1];

    for (int len = 2; len <= n; ++len) {
        //i表示起始位置 len表示区间长度
        for (int i = 1; i + len - 1 <= n; ++i) {
            //l是左端点 r是右端点
            int l = i, r = i + len - 1;
            dp[l][r] = 0x3f3f3f3f;
            for (int k = l; k < r; ++k) {
                dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r] + a[r] - a[l - 1]);
            }

        }
    }
    cout << dp[1][n];
}